package com.yb.livewykt.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.yb.livewykt.R
import com.yb.livewykt.bean.*
import com.yb.livewykt.view.ImageViewCheckable

class LiveBeautyRecyclerAdapter(val context: Context):RecyclerView.Adapter<LiveBeautyRecyclerAdapter.FilterViewHolder>() {

    private val dataList = FuBeautyEnum.getBeautyBodyByFilterType()
    private var itemClick:(Beauty) -> Unit = {}

    fun setOnItemClickListener(opera:(Beauty) ->Unit) {
        this.itemClick = opera
    }


    class FilterViewHolder(itemView:View):RecyclerView.ViewHolder(itemView){
        val imageView:ImageViewCheckable = itemView.findViewById(R.id.image_view)
        val name:TextView = itemView.findViewById(R.id.name)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FilterViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.adapter_live_beauty_layout,parent,false)
        return FilterViewHolder(view)
    }

    override fun onBindViewHolder(holder: FilterViewHolder, position: Int) {
        holder.imageView.setIcon(dataList[position].iconId)
        holder.name.text = context.resources.getString(dataList[position].nameId)
        if (dataList[position].isCheck){
            holder.name.setTextColor(context.resources.getColor(R.color.theme_bg_color,context.theme))
            holder.imageView.isChecked = true
        }else{
            holder.name.setTextColor(context.resources.getColor(R.color.white,context.theme))
            holder.imageView.isChecked = false
        }

        holder.itemView.setOnClickListener {
            dataList.forEach {
                if (it.isCheck) it.isCheck = false
            }
            dataList[position].isCheck = !dataList[position].isCheck
            notifyDataSetChanged()
            itemClick(dataList[position])
        }
    }

    override fun getItemCount(): Int = dataList.size

}